// copyright 1993 Michael B. Johnson; some portions copyright 1994, MIT
// see COPYRIGHT for reuse legalities
//


#import "RIBOpacity.h"

@implementation RIBOpacity

+ initialize { return [RIBOpacity setVersion:1], self; }

- init
{
  [super init];
  opacity[0] = 1.0;
  opacity[1] = 1.0;
  opacity[2] = 1.0;

  return self;
}

- setOpacity:(RtColor)newOpacity
{
  opacity[0] = newOpacity[0];
  opacity[1] = newOpacity[1];
  opacity[2] = newOpacity[2];
  return self;
}

- getOpacity:(RtColor *)opacityContainer
{
  (*opacityContainer)[0] = opacity[0];
  (*opacityContainer)[1] = opacity[1];
  (*opacityContainer)[2] = opacity[2];
  return self;
}

- (RtColor *)opacity {  return &opacity; }

- (BOOL)theSameAs:otherRIBCommand
{
  RtColor  *otherOpacityPtr;


  otherOpacityPtr = [otherRIBCommand opacity];
  if ((*otherOpacityPtr)[0] != opacity[0])
  {  return NO;
  }
  if ((*otherOpacityPtr)[1] != opacity[1])
  {  return NO;
  }
  if ((*otherOpacityPtr)[2] != opacity[2])
  {  return NO;
  }

  return [super theSameAs:otherRIBCommand];
}

- renderSelf:(WW3DCamera *)camera  startingAt:(RtFloat)shutterOpenTime endingAt:(RtFloat)shutterCloseTime
{
  RiOpacity(opacity);

  return self;
}

// In prman 3.4, shading parameters don't motion blur...
//- (BOOL)isMotionBlurrable { return YES; }

- (BOOL)isLerpable { return YES; }

// note: because we've made the WWSampleList "safe" for having
// multiple samples with the same data, it's perfectly valid to return
// yourself or b
- lerpWith:b by:(float)uValue
{
   id       newMe = nil;
   RtColor  aColor, bColor, newColor;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   newMe = [super lerpWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   [self getOpacity:&aColor];
   [b getOpacity:&bColor];
   newColor[0] = aColor[0] + ((bColor[0] - aColor[0]) * uValue);
   newColor[1] = aColor[1] + ((bColor[1] - aColor[1]) * uValue);
   newColor[2] = aColor[2] + ((bColor[2] - aColor[2]) * uValue);
   [newMe setOpacity:newColor];

   return newMe;
}


- lerpSelfWith:b by:(float)uValue
{
   id       newMe = nil;
   RtColor  aColor, bColor, newColor;


   if (([self class] != [b class]) || (uValue <= 0.0))
   {  return self;
   }

   if (uValue >= 1.0)
   {  return b;
   }

   [super lerpSelfWith:b by:uValue]; // this makes a copy for us

   // okay, now do the specific stuff for this class
   [self getOpacity:&aColor];
   [b getOpacity:&bColor];
   newColor[0] = aColor[0] + ((bColor[0] - aColor[0]) * uValue);
   newColor[1] = aColor[1] + ((bColor[1] - aColor[1]) * uValue);
   newColor[2] = aColor[2] + ((bColor[2] - aColor[2]) * uValue);
   [newMe setOpacity:newColor];

   return self;
}

- writeEve:(NXStream *)stream atTabLevel:(int)tab
{
   int  i;


   for (i = 0; i < tab; i++)
   {  NXPrintf(stream, "\t");
   }
   NXPrintf(stream, "Opacity {%f %f %f};", opacity[0], opacity[1], opacity[2]); 
   return self;
}

- read:(NXTypedStream*)stream 
{
    int version;
    
    [super read:stream];
    version = NXTypedStreamClassVersion(stream,"RIBOpacity");
    if (version == 0) NXReadTypes(stream,"i",&version), version=1;
    if (version == 1) {
	NXReadArray(stream, "f", 3, opacity);
    } else {
    }
    return self;
}

- write:(NXTypedStream*)stream 
{
    [super write:stream];
    NXReadArray(stream, "f", 3, opacity);
    return self;
}

@end
